#coding:utf-8
import os
from common.koufuApi import KoufuApi
from common.koufuShop20Api import KoufuShop20Api
from common.tool import Tools

__author__ = 'yangxianming'

from common.KoufuConf import KoufuConf
from common.picAddress import PicAddress
import json
import requests

'''
删除商品
'''

def case():
    #初始化必要的参数
    kou_fu_conf = KoufuConf()
    test_url = kou_fu_conf.shop20_domain
    kou_fu_shop_api = KoufuShop20Api()
    script_name = os.path.basename(__file__)
    tool = Tools
    tf = True
    test_result = ""

    ##登陆
    head = {"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"}
    p = kou_fu_shop_api.shop_login()
    r = requests.post(test_url + kou_fu_shop_api.shop_login(get_path=1) ,data=p, headers=head)
    reCookie = r.headers.get("set-cookie")
    jsessionid = reCookie[11:47]
    print(jsessionid)
    # kou_fu_conf.koufu_shop_headers["Cookie"] = "JSESSIONID="+jsessionid
    kou_fu_conf.koufu_shop_headers = {"Cookie": "JSESSIONID="+jsessionid,"Content-Type":"application/json"}

#http://koufudev.e-fuli.com/koufu-shop/goods/list?_search=false&nd=1637054649936&limit=30&page=1&sidx=&order=asc&_=1637054649386
    #获取要删除的数据
    delete_id_list = []
    list_uri = "_search=false&nd=1637054649936&limit=30&page=1&sidx=&order=asc&_=1637054649386"
    r = requests.get(test_url+"/koufu-shop/goods/list", list_uri, headers=kou_fu_conf.koufu_shop_headers)
    print(r.content.decode("utf-8"))
    res = json.loads(r.content.decode("utf-8"))
    product_list = res["page"]["list"]
    for one in product_list:
        goods_sn = one["goodsSn"]
        if goods_sn[0:2] == "AT":
            delete_id_list.append(one["id"])

    print(delete_id_list)

    for i in delete_id_list:
        ##删除测试数据
        p = kou_fu_shop_api.get_goods_delete(update_body=[i])
        r = requests.post(test_url+kou_fu_shop_api.get_goods_delete(get_path=1), json.dumps(p), headers=kou_fu_conf.koufu_shop_headers)
        print(r.json())


    #定义预期描述
    print(str(tf))
    # print(test_result)
    expect_result = "无需验证"

    #返回测试结果 第一个参数:True or False    第二个参数测试结果描述
    return tf, [test_result, expect_result]

case()